package com.lyj.io.module.sys.controller;

import com.lyj.io.annotation.SysLogAnnotation;
import com.lyj.io.common.utils.PageUtils;
import com.lyj.io.common.utils.R;
import com.lyj.io.common.dto.IdsDto;
import com.lyj.io.module.sys.dto.UserDto;
import com.lyj.io.module.sys.entity.SysUser;
import com.lyj.io.module.sys.entity.SysUserRole;
import com.lyj.io.module.sys.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * 用户相关api
 * @author: 李亚杰
 * @version: 1.0
 * @date: 2021/8/16 15:12
 */
@Api(value = "SysUserController", tags = "用户相关api")
@RestController
@RequestMapping("/api/v1/user")
public class SysUserController {

    @Autowired
    SysUserService sysUserService;

    @Autowired
    SysPermissionService sysPermissionService;

    @Autowired
    SysUserRoleService sysUserRoleService;

    @Autowired
    SysRoleService sysRoleService;

    @Autowired
    SysUserTokenService sysUserTokenService;

    @Autowired
    SysMenuService sysMenuService;

    /**
     * 查询用户列表
     * @param
     * @return com.ancient.common.utils.R
     * @author: liyajie
     * @date: 2021/11/15 18:14
     * @exception:
     * @update:
     * @updatePerson:
     **/
    @SysLogAnnotation("查询用户列表")
    @ApiOperation(value = "查询用户列表", notes = "查询用户列表")
    @PostMapping("/getUserList")
    public R getUserList(@RequestBody Map<String, Object> params) {
        PageUtils page = sysUserService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 新增用户
     * @param
     * @return com.ancient.common.utils.R
     * @author: liyajie
     * @date: 2021/11/15 18:14
     * @exception:
     * @update:
     * @updatePerson:
     **/
    @SysLogAnnotation("新增用户")
    @ApiOperation(value = "新增用户", notes = "新增用户")
    @PostMapping("/addUser")
    public R addUser(@RequestBody SysUser sysUser) {
        sysUserService.saveUser(sysUser);
        return R.ok();
    }

    /**
     * 删除用户
     * @param
     * @return com.ancient.common.utils.R
     * @author: liyajie
     * @date: 2021/11/15 18:14
     * @exception:
     * @update:
     * @updatePerson:
     **/
    @SysLogAnnotation("删除用户")
    @ApiOperation(value = "删除用户", notes = "删除用户")
    @PostMapping("/delUser")
    public R delUser(@RequestBody IdsDto idsDto) {
        String ids = idsDto.getIds();
        sysUserService.removeByIds(Arrays.asList(ids.split(",")));
        return R.ok();
    }

    /**
     * 修改用户
     * @param sysUser
     * @return com.ancient.common.utils.R
     * @author: liyajie
     * @date: 2021/10/8 15:55
     * @exception:
     * @update:
     * @updatePerson:
     **/
    @SysLogAnnotation("修改用户")
    @ApiOperation(value = "修改用户", notes = "修改用户")
    @PostMapping("/updateUser")
    public R update(@RequestBody SysUser sysUser) {
        sysUserService.updateById(sysUser);
        return R.ok();
    }

    /**
     * 登录
     * @param userDto
     * @return com.ancient.common.utils.R
     * @author: liyajie
     * @date: 2021/11/15 18:14
     * @exception:
     * @update:
     * @updatePerson:
     **/
    @ApiOperation(value = "登录", notes = "登录")
    @PostMapping("/login")
    public R login(@RequestBody UserDto userDto) {
        SysUser sysUser = sysUserService.queryUserByUsername(userDto.getUsername());
        if (null == sysUser || !sysUser.getPassword().equals(new Sha256Hash(userDto.getPassword(), sysUser.getSalt()).toHex())) {
            return R.error("账号或密码不正确");
        }
        //账号锁定
        if (sysUser.getStatus() == 0) {
            return R.error("账号已被锁定,请联系管理员");
        }
        //生成token
        R r = sysUserTokenService.createToken(sysUser.getUserId());
        // 获取用户角色
        String roleId = "-1";
        SysUserRole sysUserRole = sysUserRoleService.getRoleIdByUserId(sysUser.getUserId());
        if(null != sysUserRole){
            roleId = sysUserRole.getRoleId();
        }
        return r.put("userId", sysUser.getUserId()).put("roleId", roleId).put("userNickName", sysUser.getNickname());
    }

    /**
     * 修改用户状态
     * @param sysUser
     * @return com.ancient.common.utils.R
     * @author: liyajie
     * @date: 2021/11/15 18:14
     * @exception:
     * @update:
     * @updatePerson:
     **/
    @SysLogAnnotation("修改用户状态")
    @ApiOperation(value = "修改用户状态", notes = "修改用户状态")
    @PostMapping("/changeUserStatus")
    public R changeUserStatus(@RequestBody SysUser sysUser) {
        sysUserService.changeUserStatus(sysUser);
        return R.ok();
    }

}
